package com.hcss.sys.version.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hcss.paging.constant.PagingConstant;
import com.hcss.paging.model.QuerySchema;
import com.hcss.sys.version.model.HcSysVersion;
import com.hcss.sys.version.model.par.HcSysVersionPar;
import com.hcss.sys.version.service.HcSysVersionService;
import com.hcss.update.model.UpdateSchema;
import com.hcss.util.AjaxResult;
import com.hcss.util.BeanCopyUtil;
import com.hcss.util.EmptyUtil;
import com.hcss.util.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @描述: HcSysVersion 功能模块
 * @作者: fangtao
 * @创建日期: 2019-08-07 07:51:35
 */
@Slf4j
@RestController
@RequestMapping("hcSysVersion")
public class HcSysVersionController {

	@Autowired
    private HcSysVersionService hcSysVersionService;

    /**
     * 查询使用的schema
     * 使用.withQueryFields(QueryType type, String... queryFileds)插入查询规则和查询字段
     */
    static QuerySchema querySchema = new QuerySchema(HcSysVersion.class);

	/**
	 * 更新插入的schema，用于鉴定数据的安全性
	 */
    static UpdateSchema updateSchema = new UpdateSchema(HcSysVersion.class, null, null);

	/**
	 * HcSysVersion数据列表页面
     *
	 * @return
	 */
	@GetMapping("listPage")
	public ModelAndView listPage(){
		return new ModelAndView("/hcSysVersion/hcSysVersion-listPage");
	}
	
	/**
	 * 新增页面
	 * @return
	 */
	@GetMapping("/addorupdate")
	public ModelAndView addPage(){
		ModelAndView page = new ModelAndView("/hcSysVersion/hcSysVersion-addOrUpdatePage");
		return page;
	}
	
	/**
	 * 修改页面
	 * @param id
	 * @return
	 */
	@GetMapping("/addorupdate/{id}")
	public ModelAndView updatePage(@PathVariable("id") String id){
		ModelAndView page = new ModelAndView("/hcSysVersion/hcSysVersion-addOrUpdatePage");
		HcSysVersion hcSysVersion = hcSysVersionService.selectByPK(id);
        page.addObject("data", JSON.toJSONString(hcSysVersion));
		return page;
	}
	
	/**
     * HcSysVersion分页数据接口
     *
     * @param par 带有查询参数的对象
     * @return
     */
	@GetMapping
	public AjaxResult hcSysVersion(HcSysVersionPar par){
		par.setQuerySchema(querySchema);
        try {
        	if (par.getPagenation()) {
        		PageInfo<HcSysVersion> list = hcSysVersionService.queryPagingData(par);
                return AjaxResult.getPageHelperTableResult(list);
        	} else {
        		List<HcSysVersion> list =  hcSysVersionService.queryAllData(par);
        		return AjaxResult.getSuccessResult(list);
        	}
        } catch (Exception e) {
            log.error("数据查询出错，参数：" + par.toString(), e);
            return AjaxResult.getErrorResult(e.getMessage());
        }
	}
	
	/**
     * HcSysVersion 单个数据的获取
     * @param id HcSysVersion主键
     * @return
     */
	@GetMapping("/{id}")
	public AjaxResult getHcSysVersionById(@PathVariable("id") String id){
        HcSysVersion hcSysVersion = hcSysVersionService.selectByPK(id);
        return new AjaxResult(hcSysVersion);
	}
	
	/**
	 * HcSysVersion 的新增
	 * @param par
	 * @return
	 */
	@PostMapping
	public AjaxResult addHcSysVersion(HcSysVersionPar par) {
		if (!UpdateSchema.checkAddOrUpdate(par, updateSchema)) return AjaxResult.getMissingParamResult();
		try {
			HcSysVersion model = BeanCopyUtil.copyConvey(par, HcSysVersion.class);
			hcSysVersionService.insertModel(model);
            return new AjaxResult();
		} catch (Exception e) {
            log.error("新增操作出错，参数：[{}] ", par.toString(), e);
			return AjaxResult.getErrorResult(e.getMessage());
		}
	}
	
	/**
	 * HcSysVersion 的修改
	 * @return
	 */
	@PutMapping("/{id}")
	public AjaxResult updateHcSysVersion(@PathVariable("id") String id, HcSysVersionPar par) {
		if (!UpdateSchema.checkAddOrUpdate(par, updateSchema)) return AjaxResult.getMissingParamResult();
		try {
			HcSysVersion model = BeanCopyUtil.copyConvey(par, HcSysVersion.class);
            hcSysVersionService.updateModel(model);
            //setId
            return new AjaxResult();
		} catch (Exception e) {
            log.error("更新操作出错，参数：[{}]", par.toString(), e);
			return AjaxResult.getErrorResult(e.getMessage());
		}
    }
    
    /**
	 * 根据ids集合进行删除
	 * 使用默认的分割符号“,”
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/{ids}")
	public AjaxResult deleteByIds(@PathVariable("ids") String ids){
		if (EmptyUtil.isHaveEmpty(ids)) return AjaxResult.getMissingParamResult();
		try {
			List<String> idList = ListUtil.convey(ListUtil.split(ids, PagingConstant.defaultSplit), String.class);
			hcSysVersionService.deleteByIds(idList);
            return new AjaxResult();
		} catch (Exception e) {
            log.error("批量删除出错，参数：[{}]",ids, e);
            return AjaxResult.getErrorResult(e.getMessage());
		}
	}
	
}